﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;
using DataAccess;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class TKKeToan : MarshalByRefObject
    {
        private static SqlCommand cmd;
        private static SqlDataReader dr;
        private static SqlConnection cn;

        public TKKeToan()
        {
            cmd = null;
            dr = null;
            cn = null;
        }

        public static Entities.TKKeToan[] Select(Entities.TKKeToan km)
        {
            Entities.TKKeToan[] list = null;
            try
            {
                
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(Common.Constants.TKKeToan.sp_Select_TKKeToan, cn);
                cmd.Parameters.Add(Common.Constants.TKKeToan.HanhDong, SqlDbType.NVarChar).Value = km.HanhDong;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.TKKeToan row = new Entities.TKKeToan();
                    row.TKKeToanID = int.Parse(dr[Common.Constants.TKKeToan.TKKeToanID].ToString());
                    row.MaTKKeToan = dr[Common.Constants.TKKeToan.MaTKKeToan].ToString();
                    row.TenTaiKhoan = dr[Common.Constants.TKKeToan.TenTaiKhoan].ToString();
                    row.MaNhomTKKeToan = dr[Common.Constants.TKKeToan.MaNhomTKKeToan].ToString();
                    row.GhiChu = dr[Common.Constants.TKKeToan.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.TKKeToan.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.TKKeToan[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.TKKeToan)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
                
            }
            return list;
        }

        public static int InsertUpdate(Entities.TKKeToan km)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(Common.Constants.TKKeToan.sp_InsertUpdate_TKKeToan, cn);
                    cmd.Parameters.Add(Common.Constants.TKKeToan.HanhDong, SqlDbType.NVarChar).Value = km.HanhDong;
                    cmd.Parameters.Add(Common.Constants.TKKeToan.TKKeToanID, SqlDbType.Int).Value = km.TKKeToanID;
                    cmd.Parameters.Add(Common.Constants.TKKeToan.MaTKKeToan, SqlDbType.VarChar).Value = km.MaTKKeToan;
                    cmd.Parameters.Add(Common.Constants.TKKeToan.TenTaiKhoan, SqlDbType.NVarChar).Value = km.TenTaiKhoan;
                    cmd.Parameters.Add(Common.Constants.TKKeToan.MaNhomTKKeToan, SqlDbType.NVarChar).Value = km.MaNhomTKKeToan;
                    cmd.Parameters.Add(Common.Constants.TKKeToan.GhiChu, SqlDbType.NVarChar).Value = km.GhiChu;
                    cmd.Parameters.Add(Common.Constants.TKKeToan.Deleted, SqlDbType.Bit).Value = km.Deleted;
                    rt = cmd.ExecuteNonQuery();
                    if (rt != 0)
                    {
                        if (km.HanhDong == Common.Constants.Insert)
                        {
                            LuuVietHeThong.LuuViet
                            (
                            "Thêm",
                            km.TaiKhoan.MaNhanVien,
                            "Thêm tài khoản kế toán " + km.MaTKKeToan,
                            km.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToShortDateString()
                            );
                        }
                        else
                        {
                            LuuVietHeThong.LuuViet
                                (
                                "Sửa",
                                km.TaiKhoan.MaNhanVien,
                                "Sửa tài khoản kế toán " + km.MaTKKeToan,
                                km.TaiKhoan.TenDangNhap,
                                DateTime.Now.ToShortDateString()
                                );
                        }
                    }
                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message.ToString(); rt = 0;
            }
            return rt;
        }

        public static int Delete(Entities.TKKeToan km)
        {
            int rt = 0;
            try
            {
                
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.TKKeToan.sp_Delete_TKKeToan, cn);
                cmd.Parameters.Add(Common.Constants.TKKeToan.TKKeToanID, SqlDbType.Int).Value = km.TKKeToanID;
                rt = cmd.ExecuteNonQuery();
                if (rt != 0)
                {
                    LuuVietHeThong.LuuViet
                        (
                        "Xóa",
                        km.TaiKhoan.MaNhanVien,
                        "Xóa tài khoản kế toán " + km.MaTKKeToan,
                        km.TaiKhoan.TenDangNhap,
                        DateTime.Now.ToShortDateString()
                        );
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }
    }
}
#endregion